﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;
using KCMS.Areas.Admin.Models;
using KCMS.Areas.Admin.Engine;
namespace KCMS.Areas.Admin.Controllers
{
    [Authorize]
    public class PageController : Controller
    {
        private IPageService pService = ObjectFactory.GetInstance<IPageService>();
        private IPageCategoryService pCatService = ObjectFactory.GetInstance<IPageCategoryService>();
        private GenderListGenerator<PageCategory> genderGenerator = new GenderListGenerator<PageCategory>();
        public ActionResult Index()
        {
            ViewData["categories"] = genderGenerator.getGenderListWithEmptyOption(pCatService.getAll());
            return View(pService.getAll());
        }
        public ActionResult GetByCategory(Guid categoryId)
        {
           List<Page> pages = new List<Page>();
            if (categoryId.Equals(Guid.Empty))
            {
                pages.AddRange((from t in pService.getAll()
                         select t).ToList());
            }
            else
            {
                pages.AddRange((from t in pService.getAll()
                              where t.CategoryId.Equals(categoryId)
                              select t).ToList());
            }
            ViewData["categories"] = genderGenerator.getGenderList(pCatService.getAll());
            return PartialView("_PageList", pages);
        }
        [HttpGet]
        public ActionResult Create()
        {
            var model = new PageEditorModel();
            model.CategoryList = genderGenerator.getGenderList(pCatService.getAll(), Guid.Empty);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(PageEditorModel model)
        {
            pService.save(model.Page);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            Page page = pService.getById(id);
            var model = new PageEditorModel(page);
            model.CategoryList =genderGenerator.getGenderList(pCatService.getAll(), model.Page.CategoryId);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(PageEditorModel model)
        {

            pService.update(model.Page);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            Page model = pService.getById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(Page model)
        {
            pService.delete(model.Id);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Copy(Guid id)
        {
            Page model = pService.getById(id);
            pService.save(model);
            return RedirectToAction("Index");
        }
       
    }
}
